package com.lagou;

public class DeadLock {

    private static Object obj1 = new Object();
    private static Object obj2 = new Object();

    /*
        线程1
     */
    private static class Thread1 implements Runnable{
        //重写run方法
        public void run() {
            synchronized (obj1){
                System.out.println("线程1 拿到了 obj1 的锁！");
                try {
                    // 停顿2秒，让线程2拿到obj2的锁
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (obj2){
                    System.out.println("线程1 拿到了 obj2 的锁！");
                }
            }
        }
    }

    /*
        线程2
     */
    private static class Thread2 implements Runnable{
        //重写run方法
        public void run() {
            synchronized (obj2){
                System.out.println("线程2 拿到了 obj2 的锁！");
                try {
                    // 停顿2秒，让线程1拿到obj1的锁
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (obj1){
                    System.out.println("线程2 拿到了 obj1 的锁！");
                }
            }
        }
    }


    public static void main(String[] args) {
        new Thread(new Thread1()).start();
        new Thread(new Thread2()).start();
    }
}
